﻿/*******************************************************************   
 * 创建日期：2021.07.09 11:28:17  
 * 创 建 者：Administrator 
 * 功能描述：Player玩家控制  
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cars
{

    public enum Dirction 
    {
        Left,
        Right,
        UnKnow,
    }

    internal class PlayerController
    {

        static PlayerActor m_playerActor;

        public PlayerActor ControllerActor
        {
            get => m_playerActor;
            set => m_playerActor = value;
        }

        private PlayerController() {}

        private static PlayerController m_Insatance;
        public static PlayerController Instance
        {
            get
            {
                if (m_Insatance == null)
                {
                    m_Insatance = new PlayerController();
                }
                return m_Insatance;
            }
        }

        public void Run()
        {
            if (m_playerActor == null) return;
            if (Console.KeyAvailable)
            {
                var key = Console.ReadKey(true).Key;
                GetInput(key);
            }
            m_playerActor.Draw();
        }

        private void GetInput(ConsoleKey key)
        {
            var dic = KeyMap(key);
            switch (dic)
            {
                case Dirction.Left:
                    m_playerActor.Move(Position.left);
                    Console.Beep(3000, 10);
                    break;
                case Dirction.Right:
                    m_playerActor.Move(Position.right);
                    Console.Beep(5000, 10);
                    break;
                default:
                    break;
            }
        }
        #region HP 
        private Dirction KeyMap(ConsoleKey key)
        {
            switch (key)
            {
                case ConsoleKey.LeftArrow:
                case ConsoleKey.A:
                    return Dirction.Left;

                case ConsoleKey.RightArrow:
                case ConsoleKey.D:
                    return Dirction.Right;

                default:
                    return Dirction.UnKnow;
            }
        }
        #endregion
    }
}
